﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Manager_EditTeacher : System.Web.UI.Page
{
    CommonClass ccobj = new CommonClass();
    DBClass dbobj = new DBClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindDepart();
            bindpage();
        }
    }
    protected void bindDepart()
    {
        string strsql = "select * from Department";
        DataTable dstable = dbobj.GetDataSet(strsql, "tabdepartment");
        DropDownList1.DataSource = dstable.DefaultView;
        DropDownList1.DataTextField = dstable.Columns[1].ToString();
        DropDownList1.DataValueField = dstable.Columns[0].ToString();
        DropDownList1.DataBind();
    }
    protected void bindClass(string classid)
    {
        string strsql = "select * from Class where ClassID='" + classid + "'";
        DataTable dstable = dbobj.GetDataSet(strsql, "tabclass");
        DropDownList2.DataSource = dstable.DefaultView;
        DropDownList2.DataTextField = dstable.Columns[1].ToString();
        DropDownList2.DataValueField = dstable.Columns[0].ToString();
        DropDownList2.DataBind();
    }
    protected void bindpage()
    {
        string strsql = "select * from Teacher where TeacherID='" + Request.QueryString["TeacherID"].ToString() + "'";
        DataTable dstable = dbobj.GetDataSet(strsql, "tabteacher");
        if (dstable.Rows.Count > 0)
        {
            TextBox1.Text = dstable.Rows[0]["TeacherID"].ToString();
            TextBox2.Text = dstable.Rows[0]["TeacherName"].ToString();
            TextBox3.Text = dstable.Rows[0]["Email"].ToString();
            foreach (ListItem li in RadioButtonList1.Items)
            {
                if (li.Value.Trim() == dstable.Rows[0]["Sex"].ToString().Trim())
                    li.Selected = true;
            }
            bindClass(dstable.Rows[0]["ClassID"].ToString());
            foreach (ListItem li in DropDownList2.Items)
            {
                if (li.Value.Trim() == dstable.Rows[0]["ClassID"].ToString().Trim())
                    li.Selected = true;
            }
            string strdep = "select DepartmentID from Class where ClassID='" + dstable.Rows[0]["ClassID"].ToString() + "'";
            string departID = dbobj.ExecScalar(strdep);
            foreach (ListItem li in DropDownList1.Items)
            {
                if (li.Value.Trim() == departID.ToString().Trim())
                    li.Selected = true;
            }
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strsql = "select * from Class where DepartmentID='" + DropDownList1.SelectedValue.ToString() + "'";
        DataTable dstable = dbobj.GetDataSet(strsql, "tabclass");
        DropDownList2.DataSource = dstable.DefaultView;
        DropDownList2.DataTextField = dstable.Columns[1].ToString();
        DropDownList2.DataValueField = dstable.Columns[0].ToString();
        DropDownList2.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string strsql = "update Teacher set TeacherName='" + TextBox2.Text.Trim() + "',Sex='" + RadioButtonList1.SelectedItem.Text + "',Email='" + TextBox3.Text.Trim() + "',ClassID='" + DropDownList2.SelectedValue.ToString() + "' where TeacherID='" + TextBox1.Text.Trim() + "'";
        dbobj.ExecNonQuery(strsql);
        Response.Write(ccobj.MessageBox("更新成功！"));
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Manager/ListTeacher.aspx");
    }
}